package com.iims.market.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.constant.UserConstants;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.enums.BusinessType;
import com.iims.market.domain.ContractType;
import com.iims.market.service.IContractTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/16 16:16
 */
@Api(tags = "H-合同类型管理")
@RestController
@RequestMapping("/business/contract/type")
public class ContractTypeController extends BaseController {

    @Autowired
    private IContractTypeService ctraTypeService;

    /**
     * 获取合同类型列表[树状]
     */
    @ApiOperation("获取合同类型树列表")
    @ApiImplicitParam(value = "合同类型名称", name = "ctraTypeName", paramType = "query")
    @PreAuthorize("@ss.hasPermi('business:contractType:list')")
    @GetMapping("/list")
    public AjaxResult list(@ApiIgnore ContractType contractType) throws Exception {
        List<ContractType> list = ctraTypeService.selectContractTypeList(contractType);
        return AjaxResult.success(ctraTypeService.buildCtraTypeTreeList(list));
    }

    /**
     * 获取合同类型下拉树列表
     */
    @ApiOperation("获取合同类型下拉树列表[新建合同时选择用]")
    @ApiImplicitParam(value = "合同类型名称", name = "ctraTypeName", paramType = "query")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(@ApiIgnore ContractType contractType) throws Exception {
        List<ContractType> list = ctraTypeService.selectContractTypeList(contractType);
        return AjaxResult.success(ctraTypeService.buildCtraTypeTreeSelect(list));
    }

    /**
     * 根据合同类型编号获取详细信息
     */
    @Deprecated
    //@ApiOperation("根据合同类型编号获取详细信息")
    @PreAuthorize("@ss.hasPermi('business:contractType:query')")
    @GetMapping(value = "/{ctraTypeId}")
    public AjaxResult getInfo(@PathVariable Long ctraTypeId) {
        return AjaxResult.success(ctraTypeService.selectOne(ctraTypeId));
    }

    /**
     * 新增合同类型
     */
    @ApiOperation("新增合同类型")
    @ApiOperationSupport(ignoreParameters = {"ctraTypeId"})
    @PreAuthorize("@ss.hasPermi('business:contractType:add')")
    @Log(title = "合同类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ContractType ctraType) {
        if (UserConstants.NOT_UNIQUE.equals(ctraTypeService.checkContractTypeNameUnique(ctraType))) {
            return AjaxResult.error("新增合同类型'" + ctraType.getCtraTypeName() + "'失败，合同类型名称已存在");
        }
        return toAjax(ctraTypeService.insertContractType(ctraType));
    }

    /**
     * 修改合同类型
     */
    @ApiOperation("修改合同类型")
    @PreAuthorize("@ss.hasPermi('business:contractType:edit')")
    @Log(title = "合同类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ContractType ctraType) {
        if (UserConstants.NOT_UNIQUE.equals(ctraTypeService.checkContractTypeNameUnique(ctraType))) {
            return AjaxResult.error("修改合同类型'" + ctraType.getCtraTypeName() + "'失败，合同类型名称已存在");
        }
        return toAjax(ctraTypeService.updateContractType(ctraType));
    }

    /**
     * 删除合同类型
     */
    @ApiOperation("删除合同类型")
    @PreAuthorize("@ss.hasPermi('business:contractType:remove')")
    @Log(title = "合同类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ctraTypeId}")
    public AjaxResult remove(@ApiParam("合同类型Id") @PathVariable Long ctraTypeId) {
        if (ctraTypeService.selectChildrenCount(ctraTypeId) > 0) {
            return AjaxResult.error("请先删除所有子合同类型！");
        }
        return toAjax(ctraTypeService.deleteContractTypeById(ctraTypeId));
    }
}
